import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

  const routes = [
    {
      path:'/',
      redirect:'/login'
    },
    {
      path:'/login',
      component:Login
    },
    {
      path:'/home',
      component:Home
    }
  
  
]

const router = new VueRouter({
  routes
})

//如果用户退出登录，也就意味着用户不能访问首页（/home）了
//可以使用路由的导航守卫来实现这个功能
router.beforeEach((to,from,next)=>{
  if(to.path==='/login')return next()
  //如果我们要获取到sessionStorage中的token
  const tokenS = window.sessionStorage.getItem('token')
  //如果token不存在，我们要强制跳转
  if(!tokenS) return next('/login')
  //否则放行
  next()
})

export default router
